package EurekaAnalyzer;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.util.Vector;

import Config.GlobalLog;
/**
 * @author Xenophon
 * @version 2009-12-19
 *
 */
public class WMPageAnalyzer implements PageAnalyzer {
	private File   file;
	private String fileString;
	private String title;
	private String url;
	private String board;
	
	static private String b_id_trait = "发信人: ";
	static private String b_time_traits = "发信站: 北大未名站 \\(";

	static private String e_id_traits = " ";
	static private String e_time_traits = " ";
	
	private String GetStringFromPage()
	{
		if (!file.exists() && file.isDirectory()) {
			GlobalLog.AddLog("Error, Try to get String for directory of invalid file path:"+file.getAbsolutePath());
			return null;
		}
		try {
			BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
			byte[] buffer = new byte[bis.available()];
			bis.read(buffer);
			bis.close();
		
			String res = new String(buffer, "GB2312");
			buffer = null;
			return res;
		} catch (Exception e) {
			GlobalLog.AddLog("Error:"+file.getAbsolutePath()+"Excetion Message:"+e.getMessage());
			e.printStackTrace();
		}
		return null;
	}
	
	
	/**
	 * traits is used to find the useful string in the file string, id or time etc.
	 * Always, the next word of traits. let's say this way:
     * 				...b_traitsTargete_traits...
	 * So we get all the "target"s in the file...
	 */
	private Vector<String> GetAllInfoByTraits(String b_traits, String e_traits) {
		Vector<String> ids = new Vector<String>();
		String[] segs = fileString.split(b_traits);
		for (int i = 1; i < segs.length; i++)
			ids.add(segs[i].substring(0, segs[i].indexOf(e_traits)));
		segs = null;
		return ids;
	}
	
	public WMPageAnalyzer(File file)
	{
		this.file = file;
		this.fileString = GetStringFromPage();
		this.title = null;
		this.url = GetURL();
		this.board = null;
	}
	
	@Override
	public String GetBoard() {
		if(board==null)
		{
			if(url==null)
				url = GetURL();
			String[] segs = url.split("board=");
			board = segs[1].substring(0, segs[1].indexOf("&"));
			segs = null;
		}
		return board;
	}

	@Override
	public Vector<String> GetIDs() {
		return GetAllInfoByTraits(b_id_trait, e_id_traits);
	}

	@Override
	public Vector<String> GetTimes() {
		return GetAllInfoByTraits(b_time_traits, e_time_traits);
	}

	@Override
	public String GetTitle() {
		if (title == null) {
			String[] strs = fileString.split("<title>");
			if (strs.length == 1) {
				GlobalLog.AddLog("Can't Get title from the file");
				return null;
			}
			title = strs[1].substring(0, strs[1].indexOf("</title>"));
			strs = null;
		}
		return title;
	}

	@Override
	public String GetURL() {
		if(url==null)
		{
			String[] strs = fileString.split("本文链接: <a href='");
			if(strs.length==1)
			{
				GlobalLog.AddLog("Error:Can't get url form: "+file.getAbsolutePath());
				return null;
			}
			String[] urlsegs = strs[1].split("'>");
			if(urlsegs.length==1)
			{
				GlobalLog.AddLog("Error:Can't get url form: "+file.getAbsolutePath());
				return "An error may occur,I can't get the URL...";
			}
			url = urlsegs[1].substring(0,urlsegs[1].indexOf("</a>"));
			strs = null;
		}
		return url;
	}

	@Override
	public boolean HasNext() {
		if(fileString.indexOf("下一页</a>")!=-1)
			return true;
		return false;
	}

	@Override
	public boolean HasPre() {
		if(fileString.indexOf("上一页</a>")!=-1)
			return true;
		return false;
	}

	@Override
	public boolean IsPost() {
		if (title == null)
			title = GetTitle();
		String[] segs = title.split(":");
		if (segs[0].equals("北大未名站 同主题阅读"))
			return true;
		return false;
	}
	
	public void TheMemeoryProblem()
	{
		file = null;
	    fileString = null;
		title = null;
		url   = null;
		board = null;
	}
}
